﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MotionControl.Modules.LightSource.LightControl
{
    public class JCOptLight : IJFLight
    {

        string sCom = null;
        int ChanelNum = 0;
        string sType = "OPT";
        OPTControllerAPI lightControll = null;
        string ErrorInfo = null;

        public JCOptLight(string _scom,int _chanelnum)
        {
            sCom = _scom;
            ChanelNum = _chanelnum;
            lightControll = new OPTControllerAPI();
        }
        public string GetErrorInfo()
        {
           return ErrorInfo;
        }
        public bool GetIntensity(int chanel,out int lightVal)
        {
            lightVal = 0;
            try
            {                
                ErrorInfo = null;
                lightVal = 0;
                if (chanel < 0 || chanel > ChanelNum)
                {
                    ErrorInfo = "通道超出控制器设定范围！";
                    return false;
                }

                int state = 0;
                if (lightControll.GetChannelState(chanel, ref state) != 0)
                {
                    ErrorInfo = "获取通道状态失败！";
                    return false;
                }
                if (state != 0)
                {
                    ErrorInfo = "获取通道状态失败！";
                    return false;
                }

                int ret = lightControll.ReadIntensity(chanel, ref lightVal);
                if (ret != 0)
                {
                    ErrorInfo = "读取通道亮度失败！";
                    return false;
                }

                return true;
            }
            catch(Exception e)
            {
                ErrorInfo = "读取通道亮度失败！";
                return false;
            }
            
        }

        public bool InitLight()
        {
            try
            {
                ErrorInfo = null;
               
                int ret = lightControll.InitSerialPort(sCom);
                if (ret != 0)
                {
                    ErrorInfo = "初始化光源控制器失败！";
                    return false;
                }
                return true;
            }
            catch(Exception e)
            {
                ErrorInfo = "初始化光源控制器失败," + e.Message;
                return false;
            }
           
        }

        public bool SetIntensity(int chanel, int lightVal)
        {
            ErrorInfo = null;
            try
            {
                if (chanel < 0 || chanel > ChanelNum)
                {
                    ErrorInfo = "通道超出控制器设定范围！";
                    return false;
                }
                if (lightVal < 0 || lightVal > 255)
                {
                    ErrorInfo = "通道亮度值控制器设定范围！";
                    return false;
                }

                int ret = lightControll.SetIntensity(chanel, lightVal);
                if (ret != 0)
                {
                    ErrorInfo = "设定光源控制器亮度值失败！";
                    return false;
                }
                return true;
            }
            catch (Exception e) 
            {
                ErrorInfo = "设定光源控制器亮度值失败！";
                return false;
            }
           

            
        }

        public bool TurnOffChannel(int num)
        {       
            lightControll.TurnOffChannel(num);            
            return true;
        }
        bool TurnOnChannel(int num) {
            lightControll.TurnOnChannel(num);
            return true;
        }

        public bool DisConnect()
        {
            try
            {
                lightControll.ReleaseSerialPort();
                return true;
            }
            catch (Exception e) 
            {
                ErrorInfo = "关闭设备失败";
                return false;
            }
        }

        public string type()
        {
            return sType;
        }

        /// <summary>
        /// 当前光源com口
        /// </summary>
        /// <returns></returns>
        public string comIndex()
        {
            return sCom;
        }

        /// <summary>
        /// 当前光源的通道数
        /// </summary>
        /// <returns></returns>
        public int channels()
        {
            return ChanelNum;
        }

        bool IJFLight.TurnOnChannel(int num)
        {
            return true;
        }
    }
}
